#!/usr/bin/python
# -*- coding: utf-8  -*-
"""
Substub.py v1.00 by [[zh:user:Shizhao]]

license: GPL

"""
import os
import sys
import wikipedia, config
n=0
substub=u'{{Subst:Substub/auto}}\n'
template=u'Substub'
site=wikipedia.getSite()
def stub(page, length):

    text = page.get()
    if 'Copyvio' not in page.templates():
        if page.isEmpty():
            text=u'{{d|G1}}--~~~~'+text
            page.put(text, u"Bot提交快速删除：没有内容")
            print '%s is Empty, Not Substub. Flag speed delete tag.' % page
        elif page.isDisambig():
            print '%s is Disambig, Not Substub. ' % page
        else: 
	
            text = wikipedia.removeLanguageLinks(text, wikipedia.getSite())
            text = wikipedia.removeCategoryLinks(text, wikipedia.getSite())
            templatelist=page.templates()
            if template in templatelist:
                print '%s have tag Substub. ' % page
            else:
                text=substub+text
                page.put(text, u'Bot标记[[wikipedia:小小作品|小小作品]]：%s个字' % length)
                print '%s Flag Substub: %s words' % (page, length)
    else:
        print '%s is Copyvio!' % page
#x=site.shortpages(100, repeat = True)

#print x.group()

#for (page, length) in site.shortpages(10, repeat = True):
#    page = wikipedia.Page(site, p)
#    text = page.get()
#    length=len(text)
#    print page, length
#    print p, page, t
#    if length < 200:
#        stub(page,length)
#    else:
#        print '[[%s]] is OK: %s words' % (page, length)
#        raise
#n=0
#    while True:
for (page, date, length, loggedIn, username, comment) in site.newpages(50, repeat = True):
#    n=n+1
#    while True:
 #       page = wikipedia.Page(wikipedia.getSite(), p)
        
#        t=len(text)
    if length < 200:
        stub(page, length)
    else:
        print '%s is OK: %s words' % (page, length)
#        time.sleep(1000)    
#    raise

#try:
#    run()
#finally:
#    wikipedia.stopme()
